<?php

/**
 * Celebrio Kernel
 *
 * @copyright  Copyright (c) 2012 Celebrio Software, s.r.o.
 * @package    Kernel
 */
namespace KernelModule\LogsModule\PerformanceModule;

use KernelModule\BasePresenter;
use KernelModule\LogsModule\PerformanceModule\PerformanceLogsAzure;
use KernelModule\LogsModule\PerformanceModule\PerformanceLogsFile;
use KernelModule\LogsModule\PerformanceModule\PerformanceLogsProcessorAzure;
use KernelModule\LogsModule\PerformanceModule\PerformanceLogsProcessorFile;
use KernelModule\LogsModule\PerformanceModule\LogLevelManager;

use Celebrio\Diagnostics\Timer;
use Celebrio\Diagnostics\TimerLevel;

/**
 * Displays the performance logs
 *
 * @author     Martin Novák
 */
class DisplayPresenter extends BasePresenter {
	
	/**
	 * @var IPerformanceLogs
	 */
	static $performanceLogs = NULL;
	
	private function getPerformanceLogs(){
		if(self::$performanceLogs == NULL) {
			Timer::start("Performance logs processing",TimerLevel::OVERVIEW);
			if(AZURE){
				self::$performanceLogs = new PerformanceLogsAzure();
			} else {
				self::$performanceLogs = new PerformanceLogsFile();
			}
			Timer::stop("Performance logs processing");
		}
		return self::$performanceLogs;
	}

    public function renderDefault($process) {
    	if($process == TRUE) {
    		if(AZURE){
    			$this->template->processed = PerformanceLogsProcessorAzure::processLogs();
    		} else {
    			$this->template->processed = PerformanceLogsProcessorFile::processLogs();
    		}
    	}
    	
        $this->showMenu();
        $this->template->title = _("Performance Logs");
        $logLevel = LogLevelManager::getCurrent();
        switch ($logLevel){
        	case 30000:
        		$this->template->logLevel = "DETAILED";
        		break;
        	case 20000:
        		$this->template->logLevel = "OVERVIEW";
        		break;
        	default:
        		$this->template->logLevel = "REQUEST";
        }
        $this->template->blocks = $this->getPerformanceLogs()->getBlocksList();
    }
    
	public function renderDelete($delete) {
    	$this->setView("default");
		if(AZURE){
    		$deleted = PerformanceLogsProcessorAzure::deleteHardLogsOlderThan($delete);
    	} else {
    		$deleted = PerformanceLogsProcessorFile::deleteHardLogsOlderThan($delete);
    	}
		if(!$deleted){
    		$this->template->deleteError = _("Error occured while deleting hard timer logs.");
    	}
    	if($deleted == TRUE){
    		$this->template->deleted = _("all Timer logs deleted");
    	} else {
    		$this->template->deleted = $deleted;
    	}
    	$this->renderDefault(FALSE);
    }
    
    function renderDeletestatistics(){
    	$this->setView("default");
		if(AZURE){
    		PerformanceLogsProcessorAzure::deleteStatistics();
    	} else {
    		PerformanceLogsProcessorFile::deleteStatistics();
    	}
    	$this->renderDefault(FALSE);
    }
    
    public function renderLoglevel($level) {
    	$this->setView("default");
    	if(!LogLevelManager::setLogLevel($level)){
    		$this->template->logLevelError = _("Error occured while setting up new log level.");
    	}
    	$this->renderDefault(FALSE);
    }
  
}
